using Joy.Erp.BasicArchives.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.DependencyInjection;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace Joy.Erp.BasicArchives.Production
{
    public class ProductionOrderPreparationModelConfiguration : IModelConfiguration
    {
        public void Build(ModelBuilder builder)
        {
            builder.Entity<ProductionOrderPreparation>(b =>
            {
                b.ToTable(BasicArchivesDbProperties.DbTablePrefix + nameof(ProductionOrderPreparation), BasicArchivesDbProperties.DbSchema);
                b.ConfigureByConvention();
                b.ConfigureDocumentLineAggregateRoot();

                b.Property(x => x.ItemMasterId).IsRequired(ProductionOrderPreparationConsts.ItemMasterIdRequired);
                b.Property(x => x.ItemCode).IsRequired(ProductionOrderPreparationConsts.ItemCodeRequired);
                b.Property(x => x.ItemCode).HasMaxLength(ProductionOrderPreparationConsts.ItemCodeMaxLength);
                b.Property(x => x.ItemName).IsRequired(ProductionOrderPreparationConsts.ItemNameRequired);
                b.Property(x => x.ItemName).HasMaxLength(ProductionOrderPreparationConsts.ItemNameMaxLength);
                b.Property(x => x.ItemSpec).HasMaxLength(ProductionOrderPreparationConsts.ItemSpecMaxLength);
                b.Property(x => x.Quantity).HasColumnType(ProductionOrderPreparationConsts.QuantityColumnType);
                b.Property(x => x.IssuedQuantity).HasColumnType(ProductionOrderPreparationConsts.IssuedQuantityColumnType);

                b.HasOne(x => x.ItemMaster).WithMany().HasForeignKey(x => x.ItemMasterId).OnDelete(DeleteBehavior.Restrict);
            });
        }

        public void Entity(IAbpDbContextRegistrationOptionsBuilder options)
        {
            options.Entity<ProductionOrderPreparation>(opt =>
            {
                opt.DefaultWithDetailsFunc = query =>
                {
                    return query
                        .Include(x => x.Document)
                            .ThenInclude(x => x.OrderType)
                        .Include(x => x.Document)
                            .ThenInclude(x => x.ItemMaster)
                        .Include(x => x.ItemMaster)
                        ;
                };
            });
        }
    }
} 